﻿using OnlineMealOrdering.Common;
using OnlineMealOrdering.Domain.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OnlineMealOrdering.Domain
{
    public class ShopCarEntity : AuditableEntity
    {
        public virtual UserEntity User { get; set; }

        public virtual ICollection<ShopItemEntity> Details { get; set; }

        public void AddCar(int id, int amount, decimal price)
        {
            var food = DomainContext.Current.EntitySet<FoodEntity>().SingleOrDefault(a => a.Id == id);
            if (food == null)
                BusinessException.Create("找不到添加购物车失败,ID:{0}，请重试！", id);

            var entity = Details.SingleOrDefault(a => a.Food.Id == id);
            if (entity == null)


                Details.Add(ShopItemEntity.CreateItem(id, amount, price));
        }

        public void Remove(int id)
        {
            var food = DomainContext.Current.EntitySet<FoodEntity>().SingleOrDefault(a => a.Id == id);
            if (food == null)
                BusinessException.Create("找不到餐品ID:{0}，请重试！", id);

            var entity = Details.SingleOrDefault(a => a.Food.Id == id);
            if (entity == null)
                BusinessException.Create("删除餐品车失败ID:{0}，请重试！", id);

            this.Details.Remove(entity);
        }

        public void ChangeCar(int foodId, int amount)
        {
            var food = Details.SingleOrDefault(a => a.Food.Id == foodId);
            if (food == null)
                BusinessException.Create("购物清单修改失败ID:{0}，请重试！", foodId);

            var entity = Details.SingleOrDefault(a => a.Food.Id == foodId);
            if (entity == null)
                BusinessException.Create("更改清单车失败ID:{0}，请重试！", foodId);

            entity.Change(amount);

        }
    }
}
